import {createSlice} from "@reduxjs/toolkit";
import {useSelector} from "react-redux";
import {reqAddTrademark, reqDeleteTrademark, reqTrademarkList, reqUpdateTrademark} from "@/api/goods/trademark";
import {message} from "antd";

const trademarkSlice = createSlice({
    name:'trademark',
    initialState:{
        total:0,
        trademarkList:[]
    },
    reducers:{
        upTrademarkList(state, {payload}){
            const {trademarkList,total} = payload
            state.trademarkList = trademarkList
            state.total = total
        }
    }
})

const {upTrademarkList} = trademarkSlice.actions
//暴露state状态
export const useSelectTrademark = ()=>useSelector((state:any)=>state.trademark)

//获取trademarkList
export const getTrademarkListAsync = (pageNum:number,pageSize:number)=>{
    return async (dispatch:any)=>{
        const res:any = await reqTrademarkList(pageNum,pageSize)
        dispatch(upTrademarkList({trademarkList:res.data.records,total:res.data.total}))
    }
}
//删除品牌
export const deleteTrademarkItemAsync = (id:number)=>{
    return async (dispatch:any)=>{
        try {
            await reqDeleteTrademark(id)
            message.success('删除成功~')
        }catch (e:any) {
            message.error(e.message)
        }
    }
}
//添加品牌
export const addTrademarkItemAsync = (trademarkData:any)=>{
    return async (dispatch:any)=>{
        try {
            await reqAddTrademark(trademarkData)
            message.success('添加成功~')
        }catch (e:any) {
            message.error(e.message)
        }
    }
}
//修改品牌
export const editTrademarkItemAsync = (trademarkData:any)=>{
    return async (dispatch:any)=>{
        try {
            await reqUpdateTrademark(trademarkData)
            message.success('修改成功~')
        }catch (e:any) {
            message.error(e.message)
        }
    }
}
//暴露reducer
export default trademarkSlice.reducer
